package com.qianfeng.springcloud.order.controller;


//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼            BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  


import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.qianfeng.springcloud.order.feign.ItemFeignClient;
import com.qianfeng.springcloud.order.pojo.Order;
import com.qianfeng.springcloud.order.pojo.OrderItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by jackiechan on 2021/11/3 11:17
 *
 * @author jackiechan
 * @version 1.0
 * //TODO 给最美丽的芳姐介绍学生
 * @since 1.0
 */
@RestController
@RequestMapping("/orders")
public class OrderController {

    private ItemFeignClient itemFeignClient;

    @Autowired
    public void setItemFeignClient(ItemFeignClient itemFeignClient) {
        this.itemFeignClient = itemFeignClient;
    }

    @GetMapping("/order/{id}")
    @HystrixCommand(fallbackMethod = "kafeihaishihenbucuode",commandProperties ={@HystrixProperty(name = "execution.isolation.strategy",value = "SEMAPHORE")} )
    public Order getOrderById(@PathVariable Integer id) {

        System.err.println("order收到查询请求了"+Thread.currentThread().getName());
        Order order = new Order();
        order.setId(id);
        order.setAddress("沙阳路 38 号北科");
        OrderItem orderItem = itemFeignClient.getItemById(id + 1);
        order.setOrderItem(orderItem);
//        if (new Random().nextBoolean()) {
//            int i = 1 / 0;
//        }
        int i = 1 / 0;//模拟一直出问题
        return order;
    }

    @GetMapping("/test1")
    @HystrixCommand(fallbackMethod = "test1jiangji")
    public String test1() {
        System.err.println("test1111" + Thread.currentThread().getName());
        return itemFeignClient.test1("zhangsandsg", 30);
    }

    @GetMapping("/test2")
    @HystrixCommand(fallbackMethod = "test2jiangji")//添加了注解之后如果方法的总执行时间超过 1 秒中就会失败
    public OrderItem test2() {
        System.err.println("test22222222" + Thread.currentThread().getName());
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        OrderItem orderItem = new OrderItem();
        orderItem.setId(100);
        orderItem.setName("李四");
        orderItem.setPrice(-100);
        orderItem.setCategory("的闲工夫");
        return itemFeignClient.test2(orderItem);
    }

    @GetMapping("/test3")
    public String test3() {
        System.err.println("test333333" + Thread.currentThread().getName());
        return  "33333333";
    }


    public String test1jiangji() {
        System.err.println("test1111降级" + Thread.currentThread().getName());

        return "test1jiangji";
    }


    public OrderItem test2jiangji() {
        System.err.println("test222222降级" + Thread.currentThread().getName());
        OrderItem orderItem = new OrderItem();
        orderItem.setId(-999);
        orderItem.setName("李四");
        orderItem.setPrice(-100);
        orderItem.setCategory("的闲工夫");
        return orderItem;
    }


    public Order kafeihaishihenbucuode(Integer id, Throwable throwable) {
        System.err.println("order出现异常执行了降级的方法"+Thread.currentThread().getName());
//        System.err.println(throwable);
        Order order = new Order();
        order.setId(-id);
        order.setAddress("冤鬼路 444 号 444 楼 444 室");
        order.setOrderItem(null);
        return order;
    }
}
